﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web;

namespace DD_Extensions
{
    public partial class GroupedEntityTemplate : System.Web.DynamicData.EntityTemplateUserControl
    {
        public MetaColumn currentColumn;
        public String groupName;
        public Boolean groupHeading;

        protected override void OnLoad(EventArgs e)
        {
            // get a list of groups ordered by group name
            var groupings = from t in Table.GetScaffoldColumns(Mode, ContainerType)
                            group t by t.GetAttributeOrDefault<DisplayAttribute>().GroupName into menu
                            orderby menu.Key
                            select menu.Key;

            // loop through the groups
            foreach (var groupId in groupings)
            {
                // get columns for this group
                var columns = from c in Table.GetScaffoldColumns(Mode, ContainerType)
                              where c.GetAttributeOrDefault<DisplayAttribute>().GroupName == groupId
                              orderby c.GetAttributeOrDefault<DisplayAttribute>().GetOrder()
                              select c;

                // add group separator
                if (!String.IsNullOrEmpty(groupId))
                {
                    groupHeading = true;
                    currentColumn = columns.First();
                    groupName = groupId;
                    Control item = new _NamingContainer();
                    EntityTemplate1.ItemTemplate.InstantiateIn(item);
                    EntityTemplate1.Controls.Add(item);
                }

                // add fields
                foreach (MetaColumn column in columns)
                {
                    groupHeading = false;
                    currentColumn = column;
                    Control item = new _NamingContainer();
                    EntityTemplate1.ItemTemplate.InstantiateIn(item);
                    EntityTemplate1.Controls.Add(item);
                }
            }
        }

        protected void Label_Init(object sender, EventArgs e)
        {
            if (!groupHeading)
            {
                Label label = (Label)sender;
                label.Text = currentColumn.DisplayName;
            }
            else
            {
                Label label = (Label)sender;
                label.Text = groupName;
                var parentCell = label.GetContainerControl<HtmlTableCell>();
                parentCell.ColSpan = 2;
                parentCell.Attributes.Add("class", "DDGroupHeader");
            }
        }

        protected void DynamicControl_Init(object sender, EventArgs e)
        {
            DynamicControl dynamicControl = (DynamicControl)sender;
            dynamicControl.DataField = currentColumn.Name;
            if (groupHeading)
            {
                // hide Dynamic Control maybe overkill
                dynamicControl.Visible = false;
                // get the parent cell
                var parentCell = dynamicControl.GetContainerControl<HtmlTableCell>();
                // hide the cell
                parentCell.Visible = false;
            }
        }

        public class _NamingContainer : Control, INamingContainer { }
    }
}
